package com.cb.servicebase.exception;

import com.cb.utils.R;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常拦截
 *
 * @author cb
 * @version 1.0
 * @date 2020/6/20 16:46
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiOperation("全局的异常处理")
    @ExceptionHandler(Exception.class)
    public R error(HttpServletRequest req,Exception e){
        logger.error("Request URL : {}, Exception : {}",req.getRequestURL(),e);
        return R.error().message("服务器错误...");
    }

    @ApiOperation("edu-teacher模块的异常处理")
    @ExceptionHandler(GuliException.class)
    public R eduTeacherError(HttpServletRequest req, GuliException e){
        logger.error("Request URL : {}, Code : {}, Exception : {}", req.getRequestURL(), e.getCode(), e.getMsg());
        return R.error().code(e.getCode()).message(e.getMsg());
    }
}
